package gbench.sandbox.iching;

import org.junit.jupiter.api.Test;

import static gbench.common.matlib.MatlibCanvas.print;
import static gbench.common.matlib.MatlibCanvas.println;
import static gbench.common.tree.LittleTree.RPTA;
import static gbench.common.tree.LittleTree.IRecord.A;

import java.io.File;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicReference;

import gbench.common.fs.FileSystem;
import gbench.common.fs.XlsFile.SimpleExcel;
import gbench.common.matlib.MatlibCanvas.NVec;
import gbench.common.matlib.MatlibCanvas.Range;
import gbench.common.matlib.data.DataReader.DFrame;
import gbench.common.matlib.rdd.PVec;
import gbench.common.tree.LittleTree.IRecord;
import gbench.common.tree.LittleTree.Term;
import java.util.function.*;
import java.util.regex.Pattern;


public class IChing {
    
    /**
     * 卜蓍/筮原理
     */
    @Test
    public void bushi() {
        
        /**
         * 取49蓍，取出一根，用48，分为两组，揲四，求余数：
         * 或 4，或 4+4 = 8 的 余数 的 合数。
         * 而后 三变：
         * 用 (48 - a - b - c )/4 = 6,7,8,9
         */
        Range.cph(RPTA(3, A(4,8))) // 三次占卜 
        .collect(PVec.pvec1clc(e->NVec.of(e).sorted())).groupByKey()
        .forEach((k,v)->{
            println(k, v, (int) (48 - k.sum()), (int) (48 - k.sum()) / 4);
        });
        
    }
    
    /**
     * 五行的理解
     */
    @Test
    public void wuxing() {
        final var wxyy = excel.autoDetect("五行阴阳");
        println(wxyy);
        
        final DFrame df = excel.autoDetect("五行分布").mapByRow(IRecord::REC).collect(DFrame.dfmclc);
        println(df);
        
        final var p = df.rowsS().collect(PVec.pvec1clc(e->e.str(0)));
        println(p);
        println("火","---->",p.get("火"));
        println("火","五官","---->",p.get("火").get("五官"));
        
        final DFrame bagua = excel.autoDetect("六十四卦").mapByRow(IRecord::REC).collect(DFrame.dfmclc);
        println(bagua);
    }
    
    @Test
    public void tiangan() {
        final DFrame tg = excel.autoDetect("天干").mapByRow(IRecord::REC).collect(DFrame.dfmclc);
        final DFrame dz = excel.autoDetect("地支").mapByRow(IRecord::REC).collect(DFrame.dfmclc);
        println(tg);
        println(dz);
        final var t = tg.rowsS().collect(PVec.pvec1clc(e->e.str("名称")));
        final var d = dz.rowsS().collect(PVec.pvec1clc(e->e.str("名称")));
        final var ar = new AtomicInteger(1);
        t.foreach((i, a) -> {
            d.foreach((j, b) -> {
                
                final Supplier<String> writeLine = ()->{
                    final var name = Term.FT("$0$1", a._1(), b._1());
                    if (i % 2 == 0) { // 天干的偶数 用地支的偶数
                        if (j % 2 == 0) {
                            final var no = ar.getAndIncrement(); // 计数
                            print(no, name, "\t");
                        }
                    } else { // 天干的奇数 用地支的奇数
                        if (j % 2 == 1) {
                            final var no = ar.getAndIncrement(); // 计数
                            print(no, name, "\t");
                        }
                    }
                    return "";
                };
                
                writeLine.get(); // 结果输出
            });
            println();
        });
    }
    
    /**
     * 
     */
    @Test
    public void foo() {
        final var file = new File( FileSystem.path("iching.txt", getClass()));
        final var lines = FileSystem.utf8lines(file);
        final var ar = new AtomicReference<String>("");
        final var ai = new AtomicInteger();
        lines.map(String::strip).peek(e->ai.getAndIncrement()).forEach(e -> {
            final var pat = Pattern.compile("第[一二三四五六七八九十]+卦").matcher(e);
            if (pat.find()) {
                // println(e);
                ar.set(e);
            } else {
                final var p = Pattern.compile("([凶否祸吝厉害咎利元亨利贞福禄寿吉泰荣誉]+)").matcher(e);
                if (p.find() && !e.contains("白话")) {
                    final var w = p.group(1);
                    println(ai,w, ar.get(), e);
                }
            }
        });
    }
    
    /**
     * 五行的理解
     */
    @Test
    public void bar() {
        final var zych = excel.autoDetect("周易词汇").mapByRow(IRecord::REC).collect(PVec.pvec1clc(e->e.str("标记"))).groupByKey();
        
        //println(zych);
        zych.forEach(e->{
            final var key = e._1();
            final var lines = e._2();
            println(key);
            lines.foreach(r->{
                println("\t",r);
            });
        });
    }
    
    final String path = FileSystem.path("iching.xlsx", getClass());
    final SimpleExcel excel = new SimpleExcel(path);

}
